package com.jeesite.modules.exam.socreResult.service;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.exam.socreResult.dao.DcSupExamOrgSelectDao;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamGroup;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamGroupUnit;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamOrgSelect;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DcSupExamOrgSelectService extends CrudService<DcSupExamOrgSelectDao, DcSupExamOrgSelect> {
    @Autowired
    DcSupExamGroupUnitService dcSupExamGroupUnitService;
    @Autowired
    DcSupExamGroupService dcSupExamGroupService;

    @Override
    public List<DcSupExamOrgSelect> findList(DcSupExamOrgSelect entity) {
        return super.findList(entity);
    }

    @Override
    public DcSupExamOrgSelect get(String id) {
        return super.get(id);
    }

    @Override
    public void save(DcSupExamOrgSelect entity) {
        super.save(entity);
    }

    @Override
    public void deleteByIds(DcSupExamOrgSelect entity) {
        super.deleteByIds(entity);
    }

    @Override
    public void delete(DcSupExamOrgSelect entity) {
        super.delete(entity);
    }

    public List<DcSupExamGroup> queryGroup(DcSupExamGroup dcSupTaskGroup) {
        return dcSupExamGroupService.findList(dcSupTaskGroup);
    }

    public Page<DcSupExamGroupUnit> groupListData(DcSupExamGroupUnit dcSupTaskGroupUnit) {
        return dcSupExamGroupUnitService.findPage(dcSupTaskGroupUnit);
    }
    @Transactional(readOnly = false)
    public void saveOrg(DcSupExamGroup dcSupTaskGroup) {
        dcSupExamGroupService.save(dcSupTaskGroup);
    }
    @Transactional(readOnly = false)
    public void deleteGroupUnit(DcSupExamGroupUnit dcSupTaskGroupUnit) {
        dcSupExamGroupUnitService.delete(dcSupTaskGroupUnit);
    }

    /**
     * 保存 常用组机构
     * @param dcSupTaskGroupUnit
     */
    public void saveExamGroupUnit( String dcSupTaskGroupUnit,String groupId) {
        List<DcSupExamOrgSelect> dcSupExamSumQuotaInfos = JSONObject.parseArray(dcSupTaskGroupUnit, DcSupExamOrgSelect.class);
        String userCode = UserUtils.getUser().getUserCode();
        List<DcSupExamGroupUnit> data =new ArrayList<>();
        for (int i = 0; i < dcSupExamSumQuotaInfos.size(); i++) {
            DcSupExamGroupUnit  groupUnit =new DcSupExamGroupUnit();
            groupUnit.setGroupId(groupId);
            groupUnit.setUnitCode(dcSupExamSumQuotaInfos.get(i).getOrgCode());
            List<DcSupExamGroupUnit> unitServiceList = dcSupExamGroupUnitService.findList(groupUnit);
            if (!CollectionUtils.isEmpty(unitServiceList)){
                continue;
            }
            groupUnit.setUnitName(dcSupExamSumQuotaInfos.get(i).getOrgName());
            groupUnit.setCreateBy(userCode);
            groupUnit.setCreateDate(new Date());
            data.add(groupUnit);
        }
        dcSupExamGroupUnitService.batchSave(data);

    }

    /**
     * 弃用
     * @param dcSupTaskGroupUnit
     */
    @Transactional(readOnly = false)
    @Deprecated
    public void saveOrgByAllOrg( DcSupExamOrgSelect dcSupTaskGroupUnit) {
        // 机构代码
        String unitCodes = dcSupTaskGroupUnit.getUnitCodes();
        // 机构名称
        String unitNames = dcSupTaskGroupUnit.getUnitNames();
        // 用户信息
        String userCode = UserUtils.getUser().getUserCode();
        if (unitCodes.length() > 0) {
            // 查询最大的序号信息
            Integer maxNum = dao.getMaxSort();
            if (maxNum == null) {
                maxNum = 0;
            }
            String[] codes = unitCodes.split(",");
            String[] names = unitNames.split(",");
            for (int i = 0; i < codes.length; i++) {
                DcSupExamOrgSelect save = new DcSupExamOrgSelect();
                save.setOrgCode(codes[i]);
                List<DcSupExamOrgSelect> list = dao.findList(save);
                if (!CollectionUtils.isEmpty(list)) {
                    continue;
                }
                maxNum++;
                save.setOrgCode(codes[i]);
                save.setOrgName(names[i]);
                save.setOrgSort(maxNum);
                save.setCreateBy(userCode);
                save.setCreateDate(new Date());
                save.setOrgType(dcSupTaskGroupUnit.getOrgType());
                save(save);
            }
        }
    }

    /**
     * 删除组数据信息
     * @param dcSupExamGroup
     */
    @Transactional(readOnly = false)
    public void deleteGroup(DcSupExamGroup dcSupExamGroup) {
        dcSupExamGroupService.delete(dcSupExamGroup);
    }

    public DcSupExamOrgSelect getByOrgCode(DcSupExamOrgSelect dcSupExamOrgSelect) {
        return this.dao.getByOrgCode(dcSupExamOrgSelect);
    }
}
